package com.zhuiYun.project.api.hotSellingList.Test;/**
 * @author syc
 * @date 2023/4/5 9:28
 */
import com.zhuiYun.project.api.commoditydetail.entity.Commodity;
import com.zhuiYun.project.api.hotSellingList.service.serviceImpl.HotServiceImpl;
import com.zhuiYun.project.common.response.CommonResult;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName AopTest
 * @Description TODO
 * @Author sgo
 * @Date 2023/4/5 9:28
 **/
@Aspect
@Order(value = 999)
@Component
public class AopTest {
    private static int num =0;
    @Autowired
    HotServiceImpl hotService;
    //选择一个类或方法作为切点
    // execution(* com.wmh.controller.UserController.*(..)) 代表UserController里面 所有方法
    //execution(* com.wmh.controller.UserController.getUser()) 代表UserController里面的getUser方法
//    @Pointcut(value = "execution(* com.zhuiYun.project.api.commoditydetail.controller.CommodityDetailController.getCommodityById(Integer)) " +
//            "&& args(commendById)")
//    private void aspectPointcut(Integer commendById) {
//        System.out.println("目标方法中Integer参数为:"+commendById);
//    }

//    @Before(value = "aspectPointcut()")
//    public void aspectBefore() {
//        num++;
//        System.out.println("前置通知:"+ num);
//    }

    @AfterReturning(returning="retVal",
            pointcut="execution(* com.zhuiYun.project.api.commoditydetail.controller.CommodityDetailController.getCommodityById(Integer))" +
                    "&& args(commendById)")
    public void aspectAfter(Integer commendById,CommonResult retVal) {
        System.out.println("方法参数："+ commendById);
        System.out.println("返回对象："+retVal);
        final Commodity commodity1 = (Commodity) retVal.getData();
        System.out.println("commodity1::"+commodity1);
        num++;
        if(retVal.getCode()==200){
//            业务添加
            Commodity commodity=new Commodity();
            commodity.setNumberOfViews(commodity1.getNumberOfViews()+1);
            commodity.setId(commendById);
            System.out.println("commodity::"+commodity);
            hotService.UpdateByCommodityById(commodity);
        }

        System.out.println("后置通知:"+ num);
    }


    /**
     * 获取目标主机的ip
     * @param request
     * @return
     */
    private String getRemoteHost(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.contains("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }


}
